﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
public partial class Agency_BidGoods_detail : System.Web.UI.Page
{
    public string straa;
    string strSql, BidId, ProjectId,IsWhoId;
    protected void Page_Load(object sender, EventArgs e)
    {
        BidId =SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["BidId"]));
        ProjectId = SysFun.IsSQL(Request.QueryString["ProjectId"]);
        IsWhoId = SysFun.IsSQL(Request.QueryString["Id"]);
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumBidGoods);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            if (!IsPostBack)
            {
                    Db db = new Db();
                    System.Data.IDataReader dr;

                    strSql = "select * from v_product_goods where GoodsId in (select goodsid from project_bid where bidid=" + BidId+")";
                    dr = db.DataReader(strSql);
                    while (dr.Read())
                    {
                        txtProductNameSM.Text = dr["ProductNameSM"].ToString();
                        hidProductId.Value = dr["ProductId"].ToString();
                        labProductCode.Text = dr["ProductCode"].ToString();
                        labProductId.Text = dr["ProductId"].ToString();
                        labProductId.Visible = false;
                        labProductName.Text = dr["productname"].ToString();
                        labOutlookc.Text = dr["outlookc"].ToString();
                        labMedicinemodel.Text = dr["medicinemodel"].ToString();
                        labUnit.Text = dr["unit"].ToString();
                        txtCompany.Text = dr["companyName"].ToString();
                        txtFactor.Text = dr["Factor"].ToString();
                        //txtFactor.Enabled = false;
                        //txtGmpCode.Text = dr["GmpCode"].ToString();
                        //txtGmpDate.Text = dr["GmpDate"].ToString();
                        //txtGmpRange.Text = dr["GmpRange"].ToString();
                        //txtGmpValDate.Text = dr["GmpValDate"].ToString();
                        txtGoodsId.Text = dr["goodsid"].ToString();
                        txtGoodsId.Visible = false;
                        txtGoodsInstruction.Text = dr["GoodsInstruction"].ToString();
                        txtGoodsName.Text = dr["goodsname"].ToString();
                        txtImportCode.Text = dr["ImportCode"].ToString();
                        txtImportRegistDate.Text = dr["ImportRegistDate"].ToString();
                        txtIsSalerId.Text = dr["AccountId_SC"].ToString();
                        //txtLicence.Text = dr["Licence"].ToString();
                        //txtLicenceDate.Text = dr["LicenceDate"].ToString();
                        txtLicenseNumber.Text = dr["licensenumber"].ToString();
                        txtLicenseNumberDate.Text = dr["licensenumberdate"].ToString();
                        txtMaterial.Text = dr["Material"].ToString();
                        //txtNewMedicine.Text = dr["newmedicine"].ToString();
                        //txtNewMedicineDate.Text = dr["newmedicinedate"].ToString();
                        txtPackUnit.Text = dr["PackUnit"].ToString();
                        txtPackUnit.Visible = false;
                        rdoQualityLevels.SelectedValue = SysFun.ToTrim(dr["QualityLevels"]);
                        txtRetailPrice.Text = dr["RetailPrice"].ToString();
                        txtSurveyReportDate.Text = dr["SurveyReportDate"].ToString();
                        txtSurveyReportId.Text = dr["SurveyReportId"].ToString();

                        if (SysFun.ToTrim(dr["IsImport"]) != "")
                        {
                            chkIsImport.Checked = Convert.ToBoolean(dr["IsImport"].ToString());
                        }
                        else
                        {
                            chkIsImport.Checked = false;

                        }
                        if (chkIsImport.Checked == false)
                        {
                            straa = "none";
                        }
                        else
                        {
                            straa = "block";
                        }
                        //if (dr["isnewmedicine"].ToString() != "")
                        //{
                        //    chkIsNewMedicine.Checked = Convert.ToBoolean(dr["isnewmedicine"].ToString());
                        //}
                        //else
                        //{
                        //    chkIsNewMedicine.Checked = false;
                        //}
                        //if (dr["IsPrimitive"].ToString() != "")
                        //{
                        //    chkIsPrimitive.Checked = Convert.ToBoolean(dr["IsPrimitive"].ToString());
                        //}
                        //else
                        //{
                        //    chkIsPrimitive.Checked = false;
                        //}
                        if (dr["IsSurveyReport"].ToString() != "")
                        {
                            chkIsSurveyReport.Checked = Convert.ToBoolean(dr["IsSurveyReport"].ToString());
                        }
                        else
                        {
                            chkIsSurveyReport.Checked = false;
                        }

                        
                        txtIsalerId_dL.Text = dr["AccountId_Agent"].ToString();
                        txtIsalerId_dL.Visible = false;
                        txtIsSalerId.Text = dr["AccountId_SC"].ToString();
                        txtIsSalerId.Visible = false;
                        txtCompany_dl.Text = "";// dr["companyName_Agent"].ToString();
                        txtMemo4.Text = SysFun.ToTrim(dr["memo4"]);
                        txtMemo5.Text = dr["memo5"].ToString();
                        txtImgUrl.Text = dr["ImgUrl"].ToString();
                        txtImgUrl.Visible = false;
                        txtMemo3.Text = dr["Memo3"].ToString();
                        txtMemo3.Visible = false;
                        if (SysFun.ToTrim(dr["ImgUrl"]) == "")
                        {
                            labImg.Text = "图片未上传";
                        }
                        else
                        {
                            //Image1.ImageUrl = "~/UploadFile/" + dr["ImgUrl"];
                            labImg.Text = "<a target=_blank href='../Manage/GoodsUpdate_Img.aspx?ImgUrl=" + dr["ImgUrl"] + "'><img  src='../UploadFile/" + dr["ImgUrl"] + "' height='85px'  /></a>";

                        }
                        txtshdiscrible.Text = dr["sh_discrible"].ToString();

                    }

                    dr.Close();
                    dr.Dispose();

                    if (Request.QueryString["CheckStatus"].ToString() == "不通过")//如果审核不通过则显示
                    {
                        Panel1.Visible = true;
                        dr = db.DataReader("select MEMO9 from project_bid where BidId=" + BidId);
                        while (dr.Read())
                        {
                            TextBox1.Text = dr["MEMO9"].ToString();
                        }
                    }
                    dr.Close();
                    dr.Dispose();
                    db.Close();


               
            }
        }     
    
    
    }

    protected void Btnpass_Click(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
           // GoodsUpdate();
            string atname = _Account.DWName;
            Db db = new Db();
            strSql = "update project_bid set CheckStatus='1',memo9=null,SHSJ=getdate(),memo8='" + atname + "'  where BidId=" + BidId;
            db.Command(strSql);
           //HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "project_bid", strSql, "../Agency/BidGoods_detail.aspx");
                        
            db.Close();
            Response.Write("<script>window.opener.location.reload();window.close();</script>");
            //if (IsWhoId == "1")
            //{
            //    Response.Redirect("Check_BidGood_Child.aspx");
            //}
            //else if (IsWhoId == "2")
            //{
            //    Response.Redirect("Check_BidGood.aspx?projectid=" + ProjectId);
            //}
        }
    }
    protected void Btnnopass_Click(object sender, EventArgs e)//显示不通过原因
    {
        Panel1.Visible = true;
        TextBox1.Focus();
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
            Db db = new Db();
            string atname = _Account.DWName;
            strSql = "update project_bid set CheckStatus='0',memo9=null,SHSJ=getdate(),memo8='" + atname + "' where BidId=" + BidId;
            db.Command(strSql);
            db.Close();
            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "project_bid", strSql, "../Agency/BidGoods_detail.aspx");
            Response.Write("<script>window.opener.location.reload();window.close();</script>");
            //if (IsWhoId == "1")
            //{
            //    Response.Redirect("Check_BidGood_Child.aspx");
            //}
            //else if (IsWhoId == "2")
            //{
            //    Response.Redirect("Check_BidGood.aspx?projectid=" + ProjectId);
            //}
        }

    }
    protected void Button2_Click(object sender, EventArgs e)//保存审核不通过原因
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
            if (TextBox1.Text == "")
            {
                Response.Write("<script >alert('请填写不通过原因！');</script>");
            }
            else
            {
                Db db = new Db();
                string atname = _Account.DWName;
                strSql = "update project_bid set CheckStatus='2',MEMO9='" + TextBox1.Text.ToString().Trim() + "',SHSJ=getdate(),memo8='" + atname + "' where BidId=" + BidId;
                db.Command(strSql);
                strSql = "update goods set SH_Discrible = '" + SysFun.ToTrim(txtshdiscrible.Text) + "',CheckStatus='2',NoPass='" + TextBox1.Text.ToString().Trim() + "',shenhetime=getdate(),AccountId_SH=" + _Account.AccountId.ToString() + " where goodsid not in (select goodsid from project_bid where projectid=" + ProjectId + " and goodsid=" + txtGoodsId.Text + " and checkstatus=1) and goodsid=" + txtGoodsId.Text;
                db.Command(strSql);
                db.Close();
                Response.Write("<script>alert('审核不通过！！！');window.opener.location.reload();window.close();</script>");
                //if (IsWhoId == "1")
                //{
                //    Response.Write("<script>alert('审核不通过！！！');window.opener.location='Check_BidGood_Child.aspx';window.close();</script>");
                //}
                //else if (IsWhoId == "2")
                //{
                //    Response.Write("<script>alert('审核不通过！！！');window.opener.location='Check_BidGood.aspx?projectid=" + ProjectId + "';window.close();</script>");
                //}

                //   Response.Write("<script>parent.opener.close();parent.opener=null;parent.close();</script>");
                //  Response.Write("<script>parent.location.href='Check_BidGood.aspx?projectid=" + ProjectId + "';</script>");
                //  Response.Redirect("Check_BidGood.aspx?projectid=" + ProjectId); 
            }
        }
    }
    private void GoodsUpdate()
    {
        HZ.MBSM.Model.Goods goods = new HZ.MBSM.Model.Goods();
        if (goods != null)
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                int rowCun = 0;
                Db db1 = new Db();
                System.Data.IDataReader dr;
                strSql = "select count(GOODSID) as cun from dbo.GOODS where   PRODUCTID='" + SysFun.ToTrim(hidProductId.Value) + "'  AND  FACTOR= '" + SysFun.ToTrim(SysFun.ToInt(txtFactor.Text)) + "' AND ACCOUNTID_SC='" + SysFun.ToTrim(SysFun.ToLong(txtIsSalerId.Text.ToString())) + "' and GoodsId<>" + SysFun.ToTrim(txtGoodsId.Text);
                dr = db1.DataReader(strSql);
                if (dr.Read())
                {
                    rowCun = SysFun.ToInt(dr["cun"]);
                }
                dr.Close();
                dr.Dispose();
                db1.Close();
                if (rowCun > 0)
                {
                    Response.Write("<script language=JavaScript>alert('该商品在数据库中已经存在！！！');</script>");

                }
                else
                {
                 
                    string IsSalerId;
                    if (chkIsImport.Checked == true)
                    {
                        if (SysFun.ToTrim(Request.Form["txtIsSalerId_dl1"]) == "")
                        {
                            IsSalerId = SysFun.ToTrim(txtIsalerId_dL.Text.ToString());
                        }
                        else
                        {
                            IsSalerId = SysFun.ToTrim(Request.Form["txtIsSalerId_dl1"]);
                        }
                        goods.ImportCode = SysFun.ToTrim(txtImportCode.Text);
                        goods.AccountId_Agent = SysFun.ToLong(IsSalerId);
                        if (txtImportRegistDate.Text != "")
                            goods.ImportRegistDate = Convert.ToDateTime(txtImportRegistDate.Text.ToString());
                        else
                            goods.ImportRegistDate = Convert.ToDateTime("1900-1-1");

                    }

                    goods.GoodsId = Convert.ToInt64(txtGoodsId.Text);
                    goods.AccountId_SH = _Account.AccountId;//审核人编码，以后取Session
                    goods.AccountName_SH = _Account.AccountName;
                    goods.AccountId_SC = SysFun.ToLong(txtIsSalerId.Text.ToString());
                    
                    goods.CheckStatus = "1";
                    goods.Factor = Convert.ToInt32(txtFactor.Text.ToString());
                    goods.GoodsInstruction = txtGoodsInstruction.Text.ToString();
                    goods.GoodsName = txtGoodsName.Text.ToString();
                    goods.IsImport = chkIsImport.Checked;
                    goods.IsSurveyReport = chkIsSurveyReport.Checked;
                    goods.LicenseNumber = txtLicenseNumber.Text.ToString();
                    if (txtLicenseNumberDate.Text != "")
                        goods.LicenseNumberDate = Convert.ToDateTime(txtLicenseNumberDate.Text.ToString());
                    else
                        goods.LicenseNumberDate = Convert.ToDateTime("1900-1-1");
                    goods.Material = txtMaterial.Text.ToString();
                    goods.Pack = labOutlookc.Text.ToString() + "×" + txtFactor.Text.ToString() + labUnit.Text.ToString() + "/" + txtPackUnit.Text.ToString();
                    goods.PackUnit = txtPackUnit.Text.ToString();
                    if (hidProductId.Value != "")
                        goods.ProductId = Convert.ToInt64(hidProductId.Value.ToString());
                    goods.QualityLevels = SysFun.ToTrim(rdoQualityLevels.SelectedValue);
                    if (  txtRetailPrice.Text != "")
                        goods.RetailPrice = SysFun.ToDouble(txtRetailPrice.Text.ToString());
                    if (txtSurveyReportDate.Text != "")
                        goods.SurveyReportDate = Convert.ToDateTime(txtSurveyReportDate.Text.ToString());
                    else
                        goods.SurveyReportDate = Convert.ToDateTime("1900-1-1");
                    goods.SurveyReportId = txtSurveyReportId.Text.ToString();

                    goods.memo4 = SysFun.ToTrim(txtMemo4.Text);
                    goods.memo5 = SysFun.ToTrim(txtMemo5.Text);
                    goods.ImgUrl = SysFun.ToTrim(txtImgUrl.Text);
                    string memo3 = SysFun.ToTrim(txtMemo3.Text);
                    goods.SH_Discrible = SysFun.ToTrim(txtshdiscrible.Text);
                    goods.ProductNameSM = txtProductNameSM.Text;
                    
                    HZ.MBSM.BLL.Goods goodsOperator = new HZ.MBSM.BLL.Goods();
                    if (goodsOperator.UpdateByAdmin(goods) == true)
                        Response.Write("<script>alert('保存成功！！！');</script>");
                    
                    //保存审核说明
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "goods", "userGoodsUpdate.Goods_Update(goods)" + goods.GoodsId.ToString(), "../Magage/goodsupdate.aspx");

                   

                }

            }
            else
            {
                Response.Write("<script language=JavaScript>alert('您无权修改商品！！！');</script>");
            }

        }
    }
}
